package com.itheima.sh.test;

import com.itheima.sh.dao.UserMapper;
import com.itheima.sh.pojo.User;
import com.itheima.sh.util.SessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

public class MybatisUserTest01 {



    private static SqlSession session = null;
    private static UserMapper mapper = null;

    @BeforeClass
    public static void beaforClass(){
        //1.获取会话对象
        session = SessionFactoryUtils.getSession();
        //2.获取接口代理对象
        mapper = session.getMapper(UserMapper.class);
    }

    /*
        查询所有用户
     */
    @Test
    public void findAllUsers(){
        List<User> list = mapper.findAllUsers();
        list.forEach(System.out::println);
        //释放资源
        session.close();
    }

    /*
        定义方法实现一对多：需求2：查询id为1的用户及其订单信息
        因为这里查询id是1的用户，用户表只有一个di是1的用户，所以这里方法返回值是User
        User queryOneToMany(@Param("id") Long id);
    */
    @Test
    public void queryOneToMany(){
        User user = mapper.queryOneToMany(1L);
        System.out.println("user = " + user);
        //释放资源
        session.close();
    }

}
